<template>
  <div>
    <el-form style="width:40%;" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px"
             class="demo-ruleForm">
      <el-form-item label="原密码" prop="pwd0">
        <el-input v-model="ruleForm.pwd0" placeholder="原密码" type="password" ref="pwd0" show-password></el-input>
      </el-form-item>

      <el-form-item label="新密码" prop="pwd1">
        <el-input v-model="ruleForm.pwd1" placeholder="新密码" type="password" ref="pwd1" show-password></el-input>
      </el-form-item>

      <el-form-item label="确认新密码" prop="pwd2">
        <el-input v-model="ruleForm.pwd2" placeholder="确认新密码" type="password" ref="pwd2" show-password></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="info" @click="cancel()">取消</el-button>
        <el-button type="primary" @click="changeSubmit('ruleForm')">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
 import cookie from 'js-cookie'

  const axios = require('axios')
  export default {
    data() {
      return {
        ruleForm: {
          pwd0: '',
          pwd1: '',
          pwd2: ''
        },
        rules: {
          pwd0: [{required: true, message: '密码不能为空', trigger: 'blur'}],
          pwd1: [{required: true, message: '新密码不能为空', trigger: 'blur'}],
          pwd2: [{required: true, message: '密码必须一致', trigger: 'blur'}]
        }
      }
    },
    methods: {
      changeSubmit(formName) {
        const _this = this
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const login_name =  cookie.get('loginName_pet')
            const pwd0 = _this.$refs.pwd0.value
            const pwd1 = _this.$refs.pwd1.value
            const pwd2 = _this.$refs.pwd2.value
            console.log('编号:' + login_name)
            console.log('原密码' + pwd0)
            console.log('新密码' + pwd1)
            console.log('确认密码' + pwd2)
            axios.get('http://localhost:9000/admin/change/' + login_name + '/' + pwd0 + '/' + pwd1 + '/' + pwd2)
              .then(function (resp) {
                console.log('修改密码:' + resp)
                if (resp.data == 'success') {
                  _this.$alert('管理员<' + login_name + '>密码修改成功，请重新登录!', '消息', {
                    confirmButtonText: '确定',
                    callback: action => {
                      _this.$router.push('/login')
                    }
                  });
                } else if (resp.data == 'Original password error') {
                  _this.$alert('原密码错误，请重新输入!', '消息', {
                    confirmButtonText: '确定',
                    callback: action => {
                      /*_this.$router.push('/user/change')*/
                      window.location.reload()
                    }
                  });
                } else if (resp.data == 'Inconsistent passwords') {
                  _this.$alert('两次密码不一致!', '消息', {
                    confirmButtonText: '确定',
                    callback: action => {
                      window.location.reload()
                    }
                  });
                } else {
                  return false;
                }
              });
          } else {
            return false;
          }
        })
      },
      cancel() {
        this.$router.go(-1)
      }
    }
  }
</script>

<style scoped>

</style>
